﻿
    using System;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Shapes;
namespace WCYCReader.Drawing
{
    public class GraphicsExtendedImpl : GraphicsImpl, GraphicsExtended, Graphics
    {
        public GraphicsExtendedImpl()
        {
        }

        public GraphicsExtendedImpl(int width, int height) : base(width, height)
        {
        }

        public override void BeginBatchDrawing()
        {
            if (this.SkipNextImageRendering)
            {
                RemoveInvertGraphics(base.FrameBuffer);
            }
            else
            {
                base.BeginBatchDrawing();
            }
        }

        public bool canRenderGlyphs()
        {
            return true;
        }

        private void ChangeTopazColor(Canvas canvas, Rect rect, Brush brush, int operation)
        {
            if (!this.SkipNextImageRendering)
            {
                Rect x = new Rect();
                Rect y = new Rect();
                Path path = null;
                foreach (UIElement child in canvas.Children)
                {
                    if (!(child is Canvas))
                    {
                        if (!(child is Path))
                        {
                            continue;
                        }
                        if (operation != 2)
                        {
                            path = child as Path;
                            x.X = rect.X;
                            x.Y = rect.Y;
                            x.Width = rect.Width;
                            x.Height = rect.Height;
                            y.X = path.Data.Bounds.X;
                            y.Y = path.Data.Bounds.Y;
                            y.Width = path.Data.Bounds.Width;
                            y.Height = path.Data.Bounds.Height;
                            y = path.RenderTransform.TransformBounds(y);
                            x.Intersect(y);
                            if (x.IsEmpty || operation != 0)
                            {
                                continue;
                            }
                            path.Fill = brush;
                        }
                        else
                        {
                            System.Windows.Shapes.Rectangle rectangle = new System.Windows.Shapes.Rectangle()
                            {
                                Fill = brush,
                                Height = rect.Height,
                                Width = rect.Width
                            };
                            System.Windows.Shapes.Rectangle rectangle1 = rectangle;
                            Canvas.SetLeft(rectangle1, rect.X);
                            Canvas.SetTop(rectangle1, rect.Y);
                            canvas.Children.Insert(canvas.Children.IndexOf(child), rectangle1);
                            break;
                        }
                    }
                    else
                    {
                        this.ChangeTopazColor((Canvas)child, canvas.TransformToVisual(child).TransformBounds(rect), brush, operation);
                    }
                }
            }
        }

        public void clearRect(int x, int y, int width, int height)
        {
            //Rectangle rectangle2 = new Rectangle();
            //rectangle2.set_Fill(new SolidColorBrush(base.bgColor));
            //rectangle2.set_Height((double) height);
            //rectangle2.set_Width((double) width);
            //Rectangle element = rectangle2;
            //base.Render(element, (double) x, (double) y);
            //
            System.Windows.Shapes.Rectangle rectangle = new System.Windows.Shapes.Rectangle()
            {
                Fill = new SolidColorBrush(this.bgColor),
                Height = (double)height,
                Width = (double)width
            };
            base.Render(rectangle, (double)x, (double)y);
        }

        public void colorBlit(Rectangle rectangle, int color, int operation)
        {
            int rgb = this.getARGBColor();
            this.setARGBColor(color);
            SolidColorBrush brush = new SolidColorBrush(base.currentColor);
            Rect rect = new Rect((double) rectangle.x, (double) rectangle.y, (double) rectangle.width, (double) rectangle.height);
            this.ChangeTopazColor(base.FrameBuffer, rect, brush, operation);
            this.setARGBColor(rgb);
        }

        public void drawImage(BufferedImageExtended bufferedImage, AffineTransform transform)
        {
            if (!this.SkipNextImageRendering)
            {
                AffineTransformImpl renderTransform = null;
                if (transform != null)
                {
                    renderTransform = base.RenderTransform;
                    base.RenderTransform = (AffineTransformImpl) transform;
                }
                base.drawBufferedImage((BufferedImage) bufferedImage, 0, 0);
                if (renderTransform != null)
                {
                    base.RenderTransform = renderTransform;
                }
            }
        }

        public void drawImage(BufferedImageExtended bufferedImage, int x, int y, int width, int height, int left, int top)
        {
            Rect clip = base.Clip;
            base.Clip = new Rect((double) left, (double) top, (double) width, (double) height);
            base.drawBufferedImage((BufferedImage) bufferedImage, x - left, y - top);
            base.Clip = clip;
        }

        public override void EndBatchDrawing()
        {
            if (this.SkipNextImageRendering)
            {
                this.SkipNextImageRendering = false;
            }
            else
            {
                base.EndBatchDrawing();
            }
        }

        public void fill(GeneralPath path)
        {
            Rectangle rectangle = path.getBounds();
            Matrix matrix = base.RenderTransform.Matrix;
            Point point = base.RenderTransform.Transform(new Point((double) rectangle.x, (double) rectangle.y));
            Point point2 = base.RenderTransform.Transform(new Point((double) (rectangle.x + rectangle.width), (double) (rectangle.y + rectangle.height)));
            if (((this.PlaybackClip.Width <= 0.0) || (this.PlaybackClip.Height <= 0.0)) || (((point2.Y >= this.PlaybackClip.Y) && (point.Y <= (this.PlaybackClip.Y + this.PlaybackClip.Height))) && ((point2.X >= this.PlaybackClip.X) && (point.X <= (this.PlaybackClip.X + this.PlaybackClip.Width)))))
            {
                base.fillPath(path);
            }
        }

        public void fill(Polygon p)
        {
            base.fillPolygon(p);
        }

        public void fill(Rectangle r)
        {
            base.fillRect((int) r.getX(), (int) r.getY(), (int) r.getWidth(), (int) r.getHeight());
        }

        public int getARGBColor()
        {
            return base.getColor();
        }

        public AffineTransform getTransform()
        {
            return base.RenderTransform;
        }

        public bool needsRendering()
        {
            return !this.SkipNextImageRendering;
        }

        public static void RemoveInvertGraphics(Canvas canvas)
        {
            List<FrameworkElement> list = null;
            foreach (UIElement element in canvas.Children)
            {
                if (element is Canvas)
                {
                    RemoveInvertGraphics((Canvas) element);
                }
                else if (element is FrameworkElement)
                {
                    object obj2 = ((FrameworkElement) element).Tag;
                    if ((obj2 != null) && (obj2 == GraphicsImpl.InvertTag))
                    {
                        if (list == null)
                        {
                            list = new List<FrameworkElement>();
                        }
                        list.Add((FrameworkElement) element);
                    }
                }
            }
            if (list != null)
            {
                foreach (FrameworkElement element2 in list)
                {
                    canvas.Children.Remove(element2);
                }
            }
        }

        public void scale(double x, double y)
        {
            AffineTransformImpl renderTransform = base.RenderTransform;
            renderTransform.scale(x, y);
            base.RenderTransform = renderTransform;
        }

        public void setAntialias()
        {
        }

        public void setARGBColor(int rgb)
        {
            Color color2 = new Color();
            color2.A=((byte) ((rgb & 0xff000000L) >> 0x18));
            color2.R=((byte) ((rgb & 0xff0000) >> 0x10));
            color2.G=((byte) ((rgb & 0xff00) >> 8));
            color2.B=((byte) (rgb & 0xff));
            Color color = color2;
            base.currentColor = color;
        }

        public void setBackground(int rgb)
        {
            base.bgColor = Color.FromArgb((byte) ((rgb & 0xff000000L) >> 0x18), (byte) ((rgb & 0xff0000) >> 0x10), (byte) ((rgb & 0xff00) >> 8), (byte) (rgb & 0xff));
        }

        public void setTransform(AffineTransform aT)
        {
            base.RenderTransform = (AffineTransformImpl) aT;
        }

        public void translate(double x, double y)
        {
            AffineTransformImpl renderTransform = base.RenderTransform;
            renderTransform.translate(x, y);
            base.RenderTransform = renderTransform;
        }

        public void translate(int x, int y)
        {
            this.translate((double) x, (double) y);
        }

        internal Rect PlaybackClip { get; set; }

        public bool SkipNextImageRendering { get; set; }
    }
}

